#include "stdafx.h"

Account::Account(AxTWSLib::AxTws ^twsp)
{
	acc_opt = gcnew AccountRegularOptions;
	strg_opt = gcnew AccountStrategy1Options;
	tws_pointer = gcnew TWS(twsp);

	acc_opt->optRiskPerTrade = 50;
	acc_opt->optRiskPerDay = 300;
	acc_opt->optUseAdvisorAcc = false;
	acc_opt->optAllocation = "All";
	acc_opt->optSubAcc = 2;
	strg_opt->optTideStop = 5;
	strg_opt->optTideStopSpace = 30;
	strg_opt->optTideStopStartAfter = 70;
	strg_opt->optTriggerSpread = 0.0;
	strg_opt->optStopLossSpread = 50;
	strg_opt->optRoundANumber = 50;
	strg_opt->optRoundStopNumber = 30;
	strg_opt->optLimit_1_Dest = 1.5; strg_opt->optLimit_1_Prop = Propo::pHalf;
	strg_opt->optLimit_2_Dest = 3; strg_opt->optLimit_2_Prop = Propo::pHalf;
	strg_opt->optLimit_3_Dest = 0; strg_opt->optLimit_3_Prop = Propo::pNothing;
	strg_opt->optLimit_4_Dest = 0; strg_opt->optLimit_4_Prop = Propo::pNothing;
}

void Account::SaveDataToFile()
{
	StreamWriter^ sw = gcnew StreamWriter("Data.txt");
	sw->WriteLine(Convert::ToString(acc_opt->optRiskPerTrade));
	sw->WriteLine(Convert::ToString(acc_opt->optRiskPerDay));
	sw->WriteLine(Convert::ToString(acc_opt->optUseAdvisorAcc));
	sw->WriteLine(Convert::ToString(acc_opt->optAllocation));
	sw->WriteLine(Convert::ToString(acc_opt->optSubAcc));
	sw->WriteLine(Convert::ToString(strg_opt->optTideStop));
	sw->WriteLine(Convert::ToString(strg_opt->optTideStopSpace));
	sw->WriteLine(Convert::ToString(strg_opt->optTideStopStartAfter));
	sw->WriteLine(Convert::ToString(strg_opt->optTriggerSpread));
	sw->WriteLine(Convert::ToString(strg_opt->optStopLossSpread));
	sw->WriteLine(Convert::ToString(strg_opt->optRoundANumber));
	sw->WriteLine(Convert::ToString(strg_opt->optRoundStopNumber));
	sw->WriteLine(Convert::ToString(strg_opt->optLimit_1_Dest));
	sw->WriteLine(Convert::ToString(strg_opt->optLimit_2_Dest));
	sw->WriteLine(Convert::ToString(strg_opt->optLimit_3_Dest));
	sw->WriteLine(Convert::ToString(strg_opt->optLimit_4_Dest));
	sw->WriteLine(Convert::ToString(strg_opt->optLimit_1_Prop));
	sw->WriteLine(Convert::ToString(strg_opt->optLimit_2_Prop));
	sw->WriteLine(Convert::ToString(strg_opt->optLimit_3_Prop));
	sw->WriteLine(Convert::ToString(strg_opt->optLimit_4_Prop));
	sw->Close();
}

void Account::LoadDataFromFile(StreamReader^ sr)
{
	acc_opt->optRiskPerTrade = Convert::ToInt16(sr->ReadLine());
	acc_opt->optRiskPerDay = Convert::ToInt16(sr->ReadLine());
	acc_opt->optUseAdvisorAcc = Convert::ToBoolean(sr->ReadLine());
	acc_opt->optAllocation = sr->ReadLine();
	acc_opt->optSubAcc = Convert::ToInt16(sr->ReadLine());
	strg_opt->optTideStop = Convert::ToUInt16(sr->ReadLine());
	strg_opt->optTideStopSpace = Convert::ToDouble(sr->ReadLine());
	strg_opt->optTideStopStartAfter = Convert::ToUInt16(sr->ReadLine());
	strg_opt->optTriggerSpread = Convert::ToDouble(sr->ReadLine());
	strg_opt->optStopLossSpread = Convert::ToInt16(sr->ReadLine());
	strg_opt->optRoundANumber = Convert::ToInt16(sr->ReadLine());
	strg_opt->optRoundStopNumber = Convert::ToInt16(sr->ReadLine());
	strg_opt->optLimit_1_Dest = Convert::ToDouble(sr->ReadLine());
	strg_opt->optLimit_2_Dest = Convert::ToDouble(sr->ReadLine());
	strg_opt->optLimit_3_Dest = Convert::ToDouble(sr->ReadLine());
	strg_opt->optLimit_4_Dest = Convert::ToDouble(sr->ReadLine());
	strg_opt->optLimit_1_Prop = ConvertToProp(Convert::ToInt16(sr->ReadLine()));
	strg_opt->optLimit_2_Prop = ConvertToProp(Convert::ToInt16(sr->ReadLine()));
	strg_opt->optLimit_3_Prop = ConvertToProp(Convert::ToInt16(sr->ReadLine()));
	strg_opt->optLimit_4_Prop = ConvertToProp(Convert::ToInt16(sr->ReadLine()));

	sr->Close();
}

void Account::ChangeAccountOptParameters(AccountRegularOptions ^ac)
{
	acc_opt = ac;
}

void Account::ChangeStrategy1Parameters(AccountStrategy1Options ^lm)
{
	strg_opt = lm;
}

Propo Account::ConvertToProp(int num)
{
	switch(num)
	{
	case -1: return Propo::pNothing;
	case 0: return Propo::pAll;
	case 1: return Propo::pHalf;
	case 2: return Propo::pThird;
	case 3: return Propo::pQuarter;
	}
};

Propo Account::ConvertToProp(String ^str)
{
	if (str == "All") return Propo::pAll;
	if (str == "Half") return Propo::pHalf;
	if (str == "Third") return Propo::pThird;
	if (str == "Quarter") return Propo::pQuarter;

	return Propo::pNothing;
};

String^ Account::PropToString(Propo prp)
{
	if (prp == pNothing) return "";
	if (prp == pAll) return "All";
	if (prp == pHalf) return "Half";
	if (prp == pThird) return "Third";
	if (prp == pQuarter) return "Quarter";
}